package com.nhjf.service.impl.system;

import com.nhjf.command.organization.OrganizationCommand;
import com.nhjf.dao.system.OrganizationDao;
import com.nhjf.dao.system.UserDao;
import com.nhjf.model.system.Organization;
import com.nhjf.model.system.User;
import com.nhjf.pojo.dto.result.ServiceResultDto;
import com.nhjf.pojo.qo.system.OrganizationQo;
import com.nhjf.pojo.qo.system.UserQo;
import com.nhjf.service.impl.BaseServiceImpl;
import com.nhjf.service.system.OrganizationService;

import java.util.Date;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service("organizationService")
public class OrganizationServiceImpl extends BaseServiceImpl<Organization,OrganizationQo,OrganizationDao> implements OrganizationService {

    @Autowired
    private OrganizationDao organizationDao;
    @Autowired
    private UserDao userDao;
    @Override
    protected OrganizationDao getDao() {
        return organizationDao;
    }
	@Override
	public ServiceResultDto svaeCommand(OrganizationCommand command) {
		// TODO Auto-generated method stub
		ServiceResultDto resultDto = new ServiceResultDto();
		Organization organization=new Organization();
		if(command==null) {
			resultDto.setSuccess(false);
			resultDto.setResultMessage("非法操作");
			return resultDto;
		}
		if(command.getCity()!=null) {
			organization.setCity(command.getCity());
		}
		if(command.getCounty()!=null) {
			organization.setCounty(command.getCounty());
		}
		if(command.getProvince()!=null) {
			organization.setProvince(command.getProvince());
		}
		if(StringUtils.isNotBlank(command.getPhone())) {
			organization.setPhone(command.getPhone());
		}
		if(StringUtils.isNotBlank(command.getName())) {
			organization.setName(command.getName());
		}
		if(command.getCreateUser()!=null) {
			UserQo userQo=new UserQo();
			userQo.setSeachRole(true);
			userQo.setUserId(command.getCreateUser());
			User user=userDao.queryUnique(userQo);
			organization.setCreateUser(user.getId());
		}
		if(command.getStatus()!=null) {
			organization.setStatus(1);
		}
		if(StringUtils.isNotBlank(command.getComment())) {
			organization.setComment(command.getComment());
		}
		if(StringUtils.isNotBlank(command.getDetails())) {
			organization.setDetails(command.getDetails());
		}
		if(command.getIsopenAgencyPromotion()!=null) {
			organization.setIsopenAgencyPromotion(command.getIsopenAgencyPromotion());
		}
		organization.setCreateDate(new Date());
		organizationDao.save(organization);
		resultDto.setSuccess(true);
		return resultDto;

	}
	@Override
	public ServiceResultDto modifyCommand(OrganizationCommand command) {
		// TODO Auto-generated method stub
		ServiceResultDto resultDto = new ServiceResultDto();
		OrganizationQo organizationQo=new OrganizationQo();
		organizationQo.setId(command.getParentId());
		Organization organization=organizationDao.queryUnique(organizationQo);
		if(command==null) {
			resultDto.setSuccess(false);
			resultDto.setResultMessage("非法操作");
			return resultDto;
		}
		if(command.getCity()!=null) {
			organization.setCity(command.getCity());
		}
		if(StringUtils.isNotBlank(command.getName())) {
			organization.setName(command.getName());
		}
		if(StringUtils.isNotBlank(command.getPhone())) {
			organization.setPhone(command.getPhone());
		}
		if(command.getProvince()!=null) {
			organization.setProvince(command.getProvince());
		}
		if(command.getCounty()!=null) {
			organization.setCounty(command.getCounty());
		}
		if(StringUtils.isNotBlank(command.getComment())) {
			organization.setComment(command.getComment());
		}
		if(command.getIsopenAgencyPromotion()!=null) {
			organization.setIsopenAgencyPromotion(command.getIsopenAgencyPromotion());
		}
		if(StringUtils.isNotBlank(command.getDetails())) {
			organization.setDetails(command.getDetails());
		}
		organizationDao.update(organization);
		resultDto.setSuccess(true);
		return resultDto;
	}
}
